export const isExternal = (path: string) =>
  /^(https?:|mailto:|tel:)/.test(path);

export const isArray = (arg: any) => {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]';
  }
  return Array.isArray(arg);
};

export const isValidURL = (url: string) => {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
};

/**
 * 手机号码验证
 * @param {string} phoneNum
 * @param {string} region 手机地区 zh | hk | tw, 默认zh
 * @returns {boolean}
 */
export const isPhoneAvailable = (phoneNum: string, region: string = 'zh') => {
  let reg: RegExp;
  switch (region) {
    case 'hk':
      reg = /^([6|9])\d{7}$/;
      break;
    case 'tw':
      reg = /^09\d{8}$/;
      break;
    case 'zh':
    default:
      reg = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
      break;
  }
  return !reg.test(phoneNum);
};

/**
 * 身份证号码验证
 * @param {string} phoneNum
 * @param {string} region 地区 zh | hk | tw, 默认zh
 * @returns {boolean}
 */
export const isIdcardAvailable = (idcardNumber: string, region: string = 'zh') => {
  let reg: RegExp;
  switch (region) {
    case 'hk':
      reg = /^[A-Z]\d{6}(\([\dA]\)|（[\dA]）|[\dA])$/;
      break;
    case 'tw':
      reg = /^[A-KM-QT-XZ]{1}[0-9]{9}$/;
      break;
    case 'zh':
    default:
      reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
      break;
  }
  return !reg.test(idcardNumber);
};
